package com.lanou3g.hw0512.upload;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.List;

@WebServlet(name = "UploadServlet", urlPatterns = "/upload")
public class UploadServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        InputStream is = request.getInputStream();
//        BufferedReader reader =new BufferedReader(new InputStreamReader(is));
//        String line="";
//        while ((line=reader.readLine())!=null){
//            System.out.println(line);
//        }
//        reader.close();
        //0.修改编码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //1.检测表单是否上传文件
        if (!ServletFileUpload.isMultipartContent(request)) {
            //如果不支持上传文件,就不用往下执行了
            return;
        }
        //2.创建ServletFileUpload对象,来上传文件
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        //3.配置一些参数
        //3.1服务器支持上传的最大文件(30M)
        upload.setFileSizeMax(1024 * 1024 * 30);
        //3.2支持的 带表单中其他数据,带文件,即整个表单的最大容量
        //40M
        upload.setSizeMax(1024 * 1024 * 40);
        //3.3支持中文
        upload.setHeaderEncoding("utf-8");
        //4.需要知道  上传的文件  要放在哪---文件夹
        String path =
                //web文件夹的路径
                getServletContext().getRealPath("./")
                        + File.separator + "upload";
        File uploadDir = new File(path);
        if (!uploadDir.exists()) {
            //如果文件夹不存在,则创建文件夹
            uploadDir.mkdirs();
        }
        //5.真正的上传
        try {
            List<FileItem> fileItems = upload.parseRequest(request);
            if (fileItems != null && fileItems.size() > 0) {
                //有上传的内容
                for (FileItem fileItem : fileItems) {
                    //这个文件名  绝对不能用用户上传的那个
                    String fileName = new File(fileItem.getName()).getName();
//                    System.out.println(fileName);
//                    System.out.println(fileItem.getName());
//                    String fileName = fileItem.getName();
                    String filePath = path + File.separator + fileName;
                    File file = new File(filePath);
//                    System.out.println(filePath);
                    //将上传来的文件写入我们自己的文件中
                    fileItem.write(file);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
